package com.cloudcc.cloudframe.util;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Matrix;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

/**
 * 图片处理
 * 
 * @author wangzhiqiang
 * @date 2015-8-5 下午1:07:38
 * 
 */
public class ImageUtils {

	/**
	 * convert Bitmap to byte array
	 * 
	 * @param b
	 * @return
	 */
	public static byte[] bitmapToByte(Bitmap b) {
		if (b == null) {
			return null;
		}

		ByteArrayOutputStream o = new ByteArrayOutputStream();
		b.compress(CompressFormat.PNG, 100, o);
		return o.toByteArray();
	}

	/**
	 * convert byte array to Bitmap
	 * 
	 * @param b
	 * @return
	 */
	public static Bitmap byteToBitmap(byte[] b) {
		return (b == null || b.length == 0) ? null : BitmapFactory
				.decodeByteArray(b, 0, b.length);
	}

	/**
	 * convert Drawable to Bitmap
	 * 
	 * @param d
	 * @return
	 */
	public static Bitmap drawableToBitmap(Drawable d) {
		return d == null ? null : ((BitmapDrawable) d).getBitmap();
	}

	/**
	 * convert Bitmap to Drawable
	 * 
	 * @param b
	 * @return
	 */
	public static Drawable bitmapToDrawable(Bitmap b) {
		return b == null ? null : new BitmapDrawable(b);
	}

	/**
	 * convert Drawable to byte array
	 * 
	 * @param d
	 * @return
	 */
	public static byte[] drawableToByte(Drawable d) {
		return bitmapToByte(drawableToBitmap(d));
	}

	/**
	 * convert byte array to Drawable
	 * 
	 * @param b
	 * @return
	 */
	public static Drawable byteToDrawable(byte[] b) {
		return bitmapToDrawable(byteToBitmap(b));
	}

	/**
	 * scale image
	 * 
	 * @param org
	 * @param newWidth
	 * @param newHeight
	 * @return
	 */
	public static Bitmap scaleImageTo(Bitmap org, int newWidth, int newHeight) {
		return scaleImage(org, (float) newWidth / org.getWidth(),
				(float) newHeight / org.getHeight());
	}

	/**
	 * scale image
	 * 
	 * @param org
	 * @param scaleWidth
	 *            sacle of width
	 * @param scaleHeight
	 *            scale of height
	 * @return
	 */
	public static Bitmap scaleImage(Bitmap org, float scaleWidth,
			float scaleHeight) {
		if (org == null) {
			return null;
		}

		Matrix matrix = new Matrix();
		matrix.postScale(scaleWidth, scaleHeight);
		return Bitmap.createBitmap(org, 0, 0, org.getWidth(), org.getHeight(),
				matrix, true);
	}

	/**
	 * 保存图片
	 * 
	 * @param fileNam
	 * @param bmp
	 * @param quality
	 * @return
	 */
	public static boolean saveBitmap(String fileNam, Bitmap bmp, int quality) {
		CompressFormat format = CompressFormat.JPEG;
		File file = new File(fileNam);
		if (!file.exists()) {
			file.getParentFile().mkdirs();
		}
		OutputStream stream = null;
		try {
			stream = new FileOutputStream(fileNam);
		} catch (FileNotFoundException e) {
			return false;
		}
		return bmp.compress(format, quality, stream);
	}

	public static boolean saveBitmapForPNG(String fileNam, Bitmap bmp,
			int quality) {
		CompressFormat format = CompressFormat.PNG;
		File file = new File(fileNam);
		if (!file.exists()) {
			file.getParentFile().mkdirs();
		}
		OutputStream stream = null;
		try {
			stream = new FileOutputStream(fileNam);
		} catch (FileNotFoundException e) {
			return false;
		}
		return bmp.compress(format, quality, stream);
	}

	/**
	 * 获得bitmap
	 * 
	 * @param fileName
	 * @return
	 */
	public static Bitmap getBitmap(String fileName) {
		return BitmapFactory.decodeFile(fileName);
	}
}
